package net.sureon.web.user;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.ApplicationBO;
import net.sureon.biz.ApplicationMgr;
import net.sureon.dao.Application;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 处理提交申请
 * 
 * @author Administrator
 * 
 */
public class ProcessApplication implements Controller {
	private ApplicationMgr applicationMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long userID = 0;
		long companyID = 0;
		long groupID = 0;

		userID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();
		companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();
		groupID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getGroupID();

		
		String edit = request.getParameter("edit");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		if("edit".equals(edit)) {
			// 表示编辑申请的更新
//			if(!(sdf.parse(request.getParameter("startDate"))).before(new java.util.Date())) {
				// 编辑修改的日期不能够低于申请的开始日期
				long applicationID = Long.parseLong(request.getParameter("applicationID"));
				java.util.Date startDate = sdf.parse(request.getParameter("startDate"));
				java.util.Date endDate = sdf.parse(request.getParameter("endDate"));
				String note = request.getParameter("note");
				String category = request.getParameter("category");
				float sumTime = Float.valueOf((request.getParameter("sumTime")));
				
				applicationMgr.editApplication(companyID, applicationID, category, startDate, endDate, sumTime, note, new java.util.Date());
//			}
//		else {
//				response.sendRedirect(request.getContextPath()
//						+ "/user/myApplication.do?errorMsg=<script type='text/javascript'>不允许修改以前的申请记录</script>");
//				return null;
//			}
		} else {
			// 直接添加新的申请
			String category = request.getParameter("category");
			
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			
			if(sdf.parse(startDate).getTime() > sdf.parse(endDate).getTime()) {
				// 开始的日期大于结束的日期，不符合标准，不允许提交申请
				
			} else {
			
				String sumTime = request.getParameter("sumTime");
				String status = request.getParameter("status");
				String note = request.getParameter("note");
		
				Application application = new Application();
				application.setUserID(userID);
				application.setGroupID(groupID);
				application.setCompanyID(companyID);
				application.setCategory(category);
				application.setSubmitDate(new java.util.Date());
		
				application.setNote(note);
				application.setStatus(status);
				application.setCategory(category);
				application.setStartDate(sdf.parse(startDate));
				application.setEndDate(sdf.parse(endDate));
				application.setSumTime(Float.valueOf(sumTime));
		
				applicationMgr.insertApplication(new ApplicationBO(application));
			}
		}
		response.sendRedirect(request.getContextPath()
					+ "/user/myApplication.do");
		return null;
	}

	public ApplicationMgr getApplicationMgr() {
		return applicationMgr;
	}

	public void setApplicationMgr(ApplicationMgr applicationMgr) {
		this.applicationMgr = applicationMgr;
	}
}
